package com.sdtele.tta.operation;

import java.sql.Date;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.sdtele.tta.dac.Dac;
import com.sdtele.tta.models.JsonRequest;
import com.sdtele.tta.models.JsonResponse;
import com.sdtele.tta.models.Task;
import com.sdtele.tta.models.TaskOperation;
import com.sdtele.tta.models.TaskWrapper;
import com.sdtele.tta.models.User;
import com.sdtele.tta.utils.TeamUtils;

/**
 * 操作类实现：GetUser(json.operation=getUser)
 * 
 * @author XCream
 */
public class OperationRevokeTask extends Operation {

	@Override
	public JsonResponse handle(JsonRequest jsonRequest) {
		JsonResponse jres = new JsonResponse();
		
		User user =(User) request.getSession().getAttribute("user");
		
		Long taskId = jsonRequest.getJsonObject().get("taskId").getAsLong();
		
		Task curTask = new Task();
		
		curTask = Dac.getInstance().getTaskbyTaskid(taskId);
		//判断当前用户是否为任务创建者
		if(curTask.getOwnerId()==user.getId())
		{	//获取当前任务所有的operation
			List<TaskOperation> taskOpList = Dac.getInstance().getOperation(curTask.getId());
			//修改operation为不可见
			for(int i=0;i<taskOpList.size();i++)
			{
				if(!(user.getId()==taskOpList.get(i).getUserId()))
				{
					taskOpList.get(i).setOperation(taskOpList.get(i).getOperation()+100);
					Dac.getInstance().chgTaskoperation(taskOpList.get(i));
				}
			}
			
			//判断当前任务是否有子任务,并将全部子任务operation置为不可见
			List<Task> subTaskList = Dac.getInstance().getSubTask(taskId);
			if(!subTaskList.isEmpty())
			{
				for(int j=0;j<subTaskList.size();j++)
				{
					List<TaskOperation> subTaskOpList = Dac.getInstance().getOperation(subTaskList.get(j).getId());
					for(int k=0;k<subTaskOpList.size();k++)
					{
						
						
							subTaskOpList.get(k).setOperation(subTaskOpList.get(k).getOperation()+100);
							Dac.getInstance().chgTaskoperation(subTaskOpList.get(k));
						
					}
					
				}
			}
			
		   jres.setStatus(0);
		   jres.setMessage("撤销任务成功");
		   
		}else
		{
			jres.setStatus(1);
			jres.setMessage("撤销任务失败");
		}
	
		return jres;
	}

}
